/*
 * This servlet controls the grades for assignments. 
 */
package controllers.managecontrollers;

import classes.HibernateUtil;
import dao.AssignmentHandinDao;
import dao.CourseDao;
import dao.FileDao;
import dao.PrivateMessageDao;
import dao.StudentDao;
import dao.UserDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.AssignmentHandin;
import models.Course;
import models.Files;
import models.PrivateMessage;
import models.Student;
import models.User;

/**
 *
 * @author Achie
 */
public class ManageGradeController extends HttpServlet {

    /*
     * this method prepares the add edit grades jsp
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //---------------------
        //Load attributes
        //---------------------
        long courseid = Long.parseLong(request.getParameter("courseid"));
        long assignmentid = Long.parseLong(request.getParameter("assignmentid"));
        long studentid = Long.parseLong(request.getParameter("studentid"));
        
        request.setAttribute("courseid", courseid);
        request.setAttribute("assignmentid", assignmentid);
        request.setAttribute("studentid", studentid);

        //redirect to add_edit_grades.jsp
        RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/add_edit_grades.jsp");
        dispatcher.forward(request, response);

    }

    /*
     * This method sets/modifies a grade for an assignment
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //get parameter attributes
        long courseid = Long.parseLong(request.getParameter("courseid"));
        long assignmentid = Long.parseLong(request.getParameter("assignmentid"));
        long studentid = Long.parseLong(request.getParameter("studentid"));
        String grade = request.getParameter("grade");
        String feedback = request.getParameter("feedback");

        HibernateUtil hu = new HibernateUtil();
        AssignmentHandinDao ahd = new AssignmentHandinDao(hu.openSession());
        
        List<AssignmentHandin> assignments = ahd.findAllByStudentAndAssignment(studentid, assignmentid);
        //check if update or insert because of not handed in (1.0 as grade)

        // All for setup private message
        PrivateMessageDao pmdao = new PrivateMessageDao(hu.openSession());
        User user = (User) request.getSession().getAttribute("user"); //get the user object from the session
        UserDao ud = new UserDao(hu.openSession());
        User receiver = (User) ud.get(studentid);
        CourseDao cd = new CourseDao(hu.openSession());
        Course course = (Course) cd.get(courseid);
        
        if (assignments.size() == 1) { //update the assignment grade
            AssignmentHandin assignment = assignments.get(0);
            assignment.setGrade(grade);
            assignment.setFeedback(feedback);
            ahd.update(assignment);
            
            //send the pm
            String subject = course.getName() + ": Grade updated";
            String message = "Dear " + receiver.getFirstname() + " " + receiver.getInsertion() + " " + receiver.getLastname() + ",\n"
                    + "\n"
                    + "Your grade has been updated. Please check the details below.\n"
                    + "\n"
                    + "<table border=\"1\" class=\"grade\"><tr><td class=\"bold\">Course:</td><td>" + course.getName() + "</td></tr>"
                    + "<tr><td class=\"bold\">Assignment:</td><td>" + assignment.getAssignment().getName() + "</td></tr>"
                    + "<tr><td class=\"bold\">New grade:</td><td>" + grade + "</td></tr>"
                    + "<tr><td colspan=\"2\"><span class=\"bold\">Feedback:</span>\n" + feedback + "</td></tr></table>";
           
            PrivateMessage pm = new PrivateMessage(System.nanoTime(), subject, user, receiver, message, 0, 1, 1, receiver);
            pmdao.save(pm); //save the sent personal messsage grade
        } else { //insert a 0 (not handed in)
            long idx = System.nanoTime();
            FileDao fd = new FileDao(hu.openSession());
            Files assignment = fd.get(assignmentid);
            StudentDao sd = new StudentDao(hu.openSession());
            Student stu = sd.get(studentid);

            AssignmentHandin as = new AssignmentHandin(idx, assignment, stu, "", "#");
            as.setGrade(grade);
            as.setFeedback(feedback);
            
            ahd.save(as); //save the assignment
        }
        
        hu.close(); //close connection
        //redirect
        response.sendRedirect("HandinViewer/view?studentid=" + studentid + "&courseid=" + courseid);

    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
